﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;



namespace Dashboard
{
    class Task
    {

        public DateTime LastTime{set;get;}      //
        int intervals;          //unit: minute
        int Delaytime;            //unit: minute
        public String name { get; set; }
        public int id { get; set; }
        public String htmlFilePath;
        public String TaskFilePath;
        
        public void Refresh()
        {
            string[] sr = File.ReadAllLines(TaskFilePath);
            LastTime = DateTime.Parse(sr[0]);
            intervals = Convert.ToInt32(sr[1]);
            Delaytime = Convert.ToInt32(sr[2]);  
        }

        public DateTime GetNextTime()
        {
            return LastTime.AddMinutes(intervals);
        }

        public DateTime GetDelyTo()
        {
            return LastTime.AddMinutes(Delaytime);
        }

        public void Initial(String str)
        {
            TaskFilePath = str;
            string[] sr = File.ReadAllLines(TaskFilePath);
            LastTime = DateTime.Parse(sr[0]);
            intervals = Convert.ToInt32(sr[1]);
            Delaytime = Convert.ToInt32(sr[2]);
        }

        public int GetDelayTime()
        {
            return Delaytime;
        }

    }

    class TaskManage
    {
        static String BaseDirectory;
        static String MachineKind;
        static List<Task> TaskList;

        private TaskManage()
        {
            ;
        }

        public static int GetTaskNum()
        {
            return TaskList.Count;
        }

        public static Task GetTaskFromIndex(int index)
        {
            return TaskList[index];
        }

        static String CheckMachineKind()
        {
            return "\\FX";
        }

        static bool IsFolderRight(String name)
        {
           
            String work_name = name.Remove(0, (BaseDirectory + MachineKind).Length+1);
            if (work_name.Remove(5)=="task_")
            {
                return true;
            }
            else
            {
                return false;
            }
       
        }

        static Task BuildTask(String name)
        {
            String work_filepath = name + "\\info.txt";
            if(File.Exists(work_filepath))
            {

                Task task = new Task();
                task.Initial(work_filepath);
                task.htmlFilePath = name + "\\task.html";
                task.name = name.Remove(0, (BaseDirectory + MachineKind).Length + 1);
                return task;

            }
            else
            {
                return null;
            }        
        }


        static void LoadMachineTasks()
        {
            String Work_Directory = BaseDirectory + MachineKind;
            List<String> dirs = new List<String>(Directory.EnumerateDirectories(Work_Directory));
            int i = 0;

            foreach (String dir in dirs)
            {
                if (IsFolderRight(dir))
                {
                    Task task = BuildTask(dir);
                    if(task!=null)
                    {
                        task.id = i++;
                        TaskList.Add(task);
                    }
                }
            }
            
        }

        public static void Initial()
        {
            TaskList = new List<Task>();
            BaseDirectory = "C:\\Users\\arthurh\\Desktop\\MNS\\mns\\mns\\bin\\Debug\\MNS";

            //query the type of machine     
            MachineKind = CheckMachineKind();
            //Load the tasks of this machine
            LoadMachineTasks();     
        }

    }
}
